package com.md.youyouhui.ui.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityUserCardBinding;
import com.md.youyouhui.databinding.AdapterUserCardBinding;
import com.md.youyouhui.net.response.UserCardResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * @author Created by AdministratorZJ
 * on 2019/4/11.
 */
public class OilUserCardActivity extends BaseActivity {
    private ActivityUserCardBinding cardBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        cardBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_card);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(cardBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        adapter = new MyAdapter();
        cardBinding.oilRecycler.setAdapter(adapter);
        cardBinding.oilCheckManger.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                cardBinding.oilCheckManger.setText("取消");
                cardBinding.addOil.setVisibility(View.VISIBLE);
                adapter.upDate(true);
            } else {
                cardBinding.oilCheckManger.setText("管理");
                cardBinding.addOil.setVisibility(View.GONE);
                adapter.upDate(false);
            }
        });
        cardBinding.addOil.setOnClickListener(v -> {
            if (select == -1) {
                startActivity(new Intent(ct, AddCardActivity.class));
            } else {
                delCard();
            }
        });
    }

    private void delCard() {
        viewModel.delUserCard(delId).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    cardBinding.setProgress(false);
                    break;
                case SUCCESS:
                    cardBinding.setProgress(true);
                    select = -1;
                    cardBinding.addOil.setText("添加油卡");
                    getUserCard();
                    Toast.makeText(ct, "删除成功", Toast.LENGTH_SHORT).show();
                    break;
                case ERROR:
                    cardBinding.setProgress(true);
                    ToastUtils.errMake(OilUserCardActivity.this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserCard();
    }

    private void getUserCard() {
        viewModel.getUserCard(1, 100).observe(this, userCardResponse -> {
            assert userCardResponse != null;
            switch (userCardResponse.status) {
                case LOADING:
                    cardBinding.setProgress(false);
                    break;
                case SUCCESS:
                    cardBinding.setProgress(true);
                    UserCardResponse data = userCardResponse.data;
                    if (null == data) {
                        return;
                    }
                    List<UserCardResponse.Rows> rows = data.rows;
                    adapter.change(rows);
                    if (rows.size() == 0) {
                        select = -1;
                        cardBinding.addOil.setText("添加油卡");
                        cardBinding.oilCheckManger.setChecked(false);
                        Toast.makeText(ct, "暂无油卡", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case ERROR:
                    cardBinding.setProgress(true);
                    ToastUtils.errMake(this, userCardResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private int select = -1;
    private String delId;

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterUserCardBinding>> {
        private boolean show;
        private List<UserCardResponse.Rows> rows;

        private void change(List<UserCardResponse.Rows> rows) {
            this.rows = rows;
            notifyDataSetChanged();
        }

        private void upDate(boolean show) {
            this.show = show;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterUserCardBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterUserCardBinding userCardBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_user_card, viewGroup, false);
            return new DataBoundViewHolder<>(userCardBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterUserCardBinding> holder, int i) {
            //判断选择的油卡|
            if (select == i) {
                holder.binding.cardCheck.setChecked(true);
            } else {
                holder.binding.cardCheck.setChecked(false);
            }
            if (show) {
                //holder.binding.cardPay.setVisibility(View.GONE);
                holder.binding.cardCheck.setVisibility(View.VISIBLE);
            } else {
                //holder.binding.cardPay.setVisibility(View.VISIBLE);
                holder.binding.cardCheck.setVisibility(View.GONE);
            }
            if (rows.get(i).type.equals("1")) {
                holder.binding.cardTitle.setText("中石化");
                holder.binding.cardBg.setBackgroundResource(R.drawable.shihua);
            } else {
                holder.binding.cardTitle.setText("中石油");
                holder.binding.cardBg.setBackgroundResource(R.drawable.shiyou);
            }
            holder.binding.cardId.setText(rows.get(i).cardNo);
            //holder.binding.cardPay.setOnClickListener(v -> startActivity(new Intent(ct, PayOilActivity.class).putExtra("id", rows.get(i).cardNo)));
            holder.binding.cardCheck.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (!holder.binding.cardCheck.isPressed()) {
                    return;
                }
                if (isChecked) {
                    select = i;
                    delId = rows.get(i).id;
                    cardBinding.addOil.setText("删除油卡");
                    notifyDataSetChanged();
                } else {
                    select = -1;
                    cardBinding.addOil.setText("添加油卡");
                }
            });
        }

        @Override
        public int getItemCount() {
            return null == rows ? 0 : rows.size();
        }
    }
}
